\defmodule{AverageMathFunction}

Represents a function computing the average of several functions.
Let $f_0(x), \ldots, f_{n-1}(x)$ be a set of $n$ functions.
This function represents the average \[f(x)=\frac1n\sum_{i=0}^{n-1} f_i(x).\]

\bigskip\hrule

\begin{code}
\begin{hide}
/*
 * Class:        AverageMathFunction
 * Description:  
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       Éric Buist
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.functions;\begin{hide}

\end{hide}

public class AverageMathFunction implements MathFunction\begin{hide}

,
   MathFunctionWithFirstDerivative, MathFunctionWithDerivative,
   MathFunctionWithIntegral {
   private MathFunction[] func;
\end{hide}

   public AverageMathFunction (MathFunction... func)\begin{hide} {
      if (func == null)
         throw new NullPointerException();
      this.func = func.clone ();
   }\end{hide}
\end{code}
\begin{tabb}   Constructs a function computing the average
 of the functions in the array \texttt{func}.
\end{tabb}
\begin{htmlonly}
   \param{func}{the array of functions to average.}
\end{htmlonly}
\begin{code}

   public MathFunction[] getFunctions()\begin{hide} {
      return func.clone ();
   }\end{hide}
\end{code}
\begin{tabb}   Returns the functions being averaged.
\end{tabb}
\begin{htmlonly}
   \return{the averaged functions.}
\end{htmlonly}
\begin{code}\begin{hide}

   public double evaluate (double x) {
      double sum = 0;
      for (final MathFunction fi : func)
         sum += fi.evaluate (x);
      return sum / func.length;
   }
   
   public double derivative (double x, int n) {
      if (n < 0)
         throw new IllegalArgumentException ("n must be greater than or equal to 0");
      if (n == 0)
         return evaluate (x);
      double sum = 0;
      for (final MathFunction fi : func)
         sum += MathFunctionUtil.derivative (fi, x, n);
      return sum / func.length;
   }

   public double derivative (double x) {
      double sum = 0;
      for (final MathFunction fi : func)
         sum += MathFunctionUtil.derivative (fi, x);
      return sum / func.length;
   }

   public double integral (double a, double b) {
      double sum = 0;
      for (final MathFunction fi : func)
         sum += MathFunctionUtil.integral (fi, a, b);
      return sum / func.length;
   }
}\end{hide}
\end{code}
